﻿using SignalBox.Core;
using SignalBox.Core.UI;
using SignalBox.Core.Util;
using SignalBoxEditor.Controllers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;

namespace SignalBoxEditor
{
    public partial class frmSignalboxEditor : Form
    {
        private EditorController editorController;

        public frmSignalboxEditor()
        {
            InitializeComponent();
        }

        public LayoutPanel LayoutPanel
        {
            get { return layoutPanel1; }
        }

        private void btnNewTrack_Click(object sender, EventArgs e)
        {
            editorController.NewLayout();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            editorController = new EditorController(this);
            editorController.UpdateStatus += editorController_UpdateStatus;
            editorController.UpdateCoords += editorController_UpdateCoords;
            layoutPanel1.TrackLayout = editorController.TrackLayout;
            fillRibbonToolbox();

            
        }

        void editorController_UpdateCoords(string message)
        {
            lblCoords.Text = message;
        }

        void editorController_UpdateStatus(string message)
        {
            lblStatus.Text = message;
        }

        

        private void fillRibbonToolbox()
        {

            RibbonButton rb;
            rb = new RibbonButton();
            rb.CheckedGroup = "Toolbox";
            rb.CheckOnClick = true;
            rb.Checked = true;
            rb.Image = SignalBoxEditor.Properties.Resources.MouseCursor;
            rb.Tag = SignalBox.Core.TRACKCELLTYPE.SELECT;
            rb.Click += rb_Click;

            ribbonButtonList1.Buttons.Add(rb);

            rb = new RibbonButton();
            rb.CheckedGroup = "Toolbox";
            rb.CheckOnClick = true;
            rb.Checked = false;
            rb.Image = SignalBoxEditor.Properties.Resources.LinkTrack;
            rb.Tag = SignalBox.Core.TRACKCELLTYPE.LINKTRACK;
            rb.Click += rb_Click;


            ribbonButtonList1.Buttons.Add(rb);

            foreach (TRACKCELLTYPE tct in TrackCellType.TrackCellTypes.Keys)
            {
                TrackCellType trackCell = TrackCellType.TrackCellTypes[tct];
                rb = new RibbonButton();
                rb.CheckedGroup = "Toolbox";
                rb.CheckOnClick = true;
                rb.Checked = false;
                Bitmap bm = trackCell.GetToolboxImage();
                bm = BitmapUtils.MakeTransparent(bm, Color.White);
                rb.Image = bm;
                rb.Tag = tct;
                rb.Click += rb_Click;

                ribbonButtonList1.Buttons.Add(rb);
            }

        }

        void rb_Click(object sender, EventArgs e)
        {
            RibbonButton rb = (RibbonButton)sender;
            editorController.CurrentTrackTool = (TRACKCELLTYPE)rb.Tag;
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Signalbox Track Files|*.stf|All Files|*.*";
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                FileInfo fi = new FileInfo(dlg.FileName);
                editorController.LoadLayout(fi);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!editorController.TrackLayout.HasFileName())
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.Filter = "Signalbox Track Files|*.stf|All Files|*.*";
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    FileInfo fi = new FileInfo(dlg.FileName);
                    editorController.SaveLayout(fi);
                }
            }
            else { editorController.SaveLayout(); }
        }

        private void btnValidate_Click(object sender, EventArgs e)
        {
            editorController.ValidateLayout();
        }

        private void btnLayoutSettings_Click(object sender, EventArgs e)
        {
            editorController.LayoutSettings();
        }

        private void btnNewTimeTable_Click(object sender, EventArgs e)
        {
            editorController.NewTimetable();
        }

        private void btnCut_Click(object sender, EventArgs e)
        {
            editorController.Cut(null, null);
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            editorController.Copy(null, null);
        }

        private void btnOpenTimetable_Click(object sender, EventArgs e)
        {
            editorController.OpenTimetable();
        }

        
    }
}
